package com.xoa.util.treeUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.xoa.model.file.FileSortModel;
import com.xoa.util.common.L;


/**
 * 
 * @ClassName (类名):  FileSortTreeUtil
 * @Description(简述): 生成树结构工具类
 * @author(作者):      ys
 * @date(日期):        2017-4-17 下午4:32:41
 *
 */
public class FileCommSortTreeUtil {
	private final static String MENU_ID = "";
	
	
	Map<String, Object> map;
	List<FileSortModel> rootTree;
	List<FileSortModel> childTree;
	
	public FileCommSortTreeUtil(List<FileSortModel> rootTree,List<FileSortModel> childTree,Map<String, Object> map){
		this.rootTree = rootTree;
		this.childTree = childTree;
		this.map=map;
	}  
	
	
	public List<TreeNode> getTreeNode(){
		return getRootNodes();
	}
	
	
     /**
      * @author 杨  胜
      * @return  List<TreeNode>
      */
	private List<TreeNode>  getRootNodes(){
		List<TreeNode> rootNodes = new ArrayList<TreeNode>();
		for(FileSortModel fs : rootTree){
			TreeNode node = RootToNode(fs);
			if(node != null){
				if(addChlidNodes(node)){
					rootNodes.add(node);
				}
			}
		}
		return rootNodes;
	}
	
	/**
	 * @author 杨  胜
	 * @param fs
	 * @return
	 */
	private TreeNode RootToNode(FileSortModel fs){
		if(fs == null){
			return null;
		}
		TreeNode node = new TreeNode();
		node.setId(String.valueOf(fs.getSortId()));
		node.setDataId(String.valueOf(fs.getSortId()));
		node.setText(fs.getSortName());
		node.setParentId(String.valueOf(fs.getSortParent()));
		node.setState("closed");
		node.getAttributes().put("pid",String.valueOf(fs.getSortParent()));
		//访问权限
		node.getAttributes().put("USER_ID", CheckTree.checkAll(fs.getUserId(), map)==true?1:0);
		//有新建权限
		node.getAttributes().put("NEW_USER", CheckTree.checkAll(fs.getNewUser(), map)==true?1:0);
		//有编辑权限
		node.getAttributes().put("MANAGE_USER",  CheckTree.checkAll(fs.getManagerUser(), map)==true?1:0);
		//有删除权限
	    node.getAttributes().put("DEL_USER",  CheckTree.checkAll(fs.getDelUser(), map)==true?1:0);
		//有下载/打印权限
		node.getAttributes().put("DOWN_USER",  CheckTree.checkAll(fs.getDownUser(), map)==true?1:0);
		//有签阅权限
		node.getAttributes().put("SIGN_USER", CheckTree.checkAll(fs.getSignUser(), map)==true?1:0);
		//评论/打分权限
		node.getAttributes().put("REVIEW",  CheckTree.checkAll(fs.getReview(), map)==true?1:0);
		//所有者
		node.getAttributes().put("OWNER",  CheckTree.checkAll(fs.getOwner(), map)==true?1:0);
		return node;
	}
	
	/**
	 * @author 杨  胜       递归生成多层菜单
	 * @param rootNode
	 * @return
	 */
	private boolean addChlidNodes(TreeNode rootNode){
		List<TreeNode> childNodes = new ArrayList<TreeNode>();
		boolean  flag=rootNode.getAttributes().get("USER_ID").toString().equals("1");
		boolean  temp=false;
		boolean  temp2=false;
		for(FileSortModel fs: childTree){
			if(rootNode.getDataId().equals(String.valueOf(fs.getSortParent()))){
				TreeNode node = RootToNode(fs);
				if(flag){
					childNodes.add(node);
					temp=addChlidNodes(node);
				}else{
					if(node.getAttributes().get("USER_ID").toString().equals("1")){
					 childNodes.add(node);
					 temp2=addChlidNodes(node);
					}
				}
			}
		}
		rootNode.setChildren(childNodes);
		return flag||temp||temp2;
	}
	
}
